home *** CD-ROM | disk | FTP | other *** search
- Path: news.clark.net!not-for-mail
- From: gusty@clark.net (Harlan Messinger)
- Newsgroups: comp.lang.c++
- Subject: Re: How to initialize an array of structures?
- Date: 11 Jan 1996 19:45:17 GMT
- Organization: Clark Internet Services, Inc., Ellicott City, MD USA
- Message-ID: <4d3pcd$uj@clarknet.clark.net>
- References: <4d240e$nk5@jupiter.planet.net> <4d358f$kj9@tahko.lpr.carel.fi>
- NNTP-Posting-Host: explorer.clark.net
- Mime-Version: 1.0
- Content-Type: TEXT/PLAIN; charset=ISO-8859-1
- Content-Transfer-Encoding: 8bit
- X-Newsreader: TIN [UNIX 1.3 950726BETA PL0]
-
- Ari Lukumies (aril@cmt.lpr.mail.carel.fi) wrote:
- : Chris Kemp <chrisk@paladn.com> wrote:
- :
- : >Could someone explain how to do this properly:
- :
- : > struct FUNCTIONMAP
- : > {
- : > char *functionname;
- : > int functionnumber;
- : > int maxargs;
- : > int functarg[5];
- : >
- : > };
- : >
- : > static struct FUNCTIONMAP functionlist[300];
- : > functionlist[1]={"firstfunction",1,3,2,2,2,0,0} <-- hangs on this line
- : >
- : >TIA
- :
- : You would want to use braces:
- :
- : static struct FUNCTIONMAP functionlist[300];
- :
- : functionlist[0] = { "firstfunction", 1, 3, { 2, 2, 2, 0, 0 } };
-
- !!!!! NO !!!!!
-
- The braces {} convention only works as an initializer (that is, as part
- of the same statement that contains the declaration). It cannot be used to
- assign to an existing object. And even as an initializer, it cannot be
- used to initialize arbitrary elements of the array. It is used to fill
- the array beginning at the first element, until the initializer list is
- exhausted.
-
- The only ways to set up a convenient assignment like this are to create
- an assign member function:
-
- FUNCTIONMAP& assign (const char *fname,
- int num,
- int maxa,
- int arg1, int arg2, int arg3,
- int arg4, int arg5) {
- functionname = new char[strlen(fname)];
- strcpy(functionname, fname);
- functionnumber = num;
- [... and so forth ...]
- }
-
- or a non-empty constructor in addition to an empty constructor:
-
- public:
- FUNCTIONMAP() {}
- FUNCTIONMAP(const char *fname,
- int num,
- int maxa,
- int arg1, int arg2, int arg3,
- int arg4, int arg5) {
- functionname = new char[strlen(fname)];
- strcpy(functionname, fname);
- functionnumber = num;
- [... and so forth ...]
- }
-
- Then you could use
-
- functionlist[1].assign("firstfunction", 1, 3, 2, 2, 2, 0, 0)
-
- or
-
- functionlist[1] = FUNCTIONMAP("firstfunction", 1, 3, 2, 2, 2, 0, 0)
-
- respectively.
-
- In the second case, you might want to overload the assignment operator as
- well.
-
-